package com.xyw.code.platform.webapps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyw.code.platform.common.entity.dto.DictDTO;
import com.xyw.code.platform.common.entity.enums.CommonStatusEnum;
import com.xyw.code.platform.common.entity.po.SysDict;
import com.xyw.code.platform.webapps.dao.SysDictMapper;
import com.xyw.code.platform.webapps.service.ISysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author xyw
 * @since 2020-02-07
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Override
    public IPage<SysDict> selectDictList(int page, int pageSize, String detailName, String value, String type) {
        Page<SysDict> dictPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<SysDict> wrappers = Wrappers.<SysDict>lambdaQuery();
        wrappers.eq(SysDict::getDelFlag, CommonStatusEnum.NORMAL.getType());
        if(StringUtils.isBlank(detailName)){
            //如果为空 则是查最上级的
            wrappers.isNull(SysDict::getValue);
        }else{
            wrappers.eq(SysDict::getName,detailName);
            wrappers.isNotNull(SysDict::getValue);
        }

        //根据条件过滤
        if(StringUtils.isBlank(type) || "字典名称".equals(type)){
            if(StringUtils.isNotBlank(value)){
                wrappers.like(SysDict::getName,value);
            }
        }else{
            if(StringUtils.isNotBlank(value)){
                wrappers.like(SysDict::getDescription,value);
            }
        }

        IPage<SysDict> sysDictIPage = baseMapper.selectPage(dictPage,wrappers);
        return sysDictIPage;
    }


    @Override
    public boolean updateDict(DictDTO dictDto) {
        //如果value为空 表示更新的是最外层的  因此所有子集的name都要更新
        if(StringUtils.isBlank(dictDto.getValue())) {
            List<SysDict> sysDicts = baseMapper.selectList(Wrappers.<SysDict>lambdaQuery().select(SysDict::getId)
                    .eq(SysDict::getName, baseMapper.selectById(dictDto.getId()).getName()));
            //更新所有该name的
            List<SysDict> collect = sysDicts.stream().map(sonDict -> {
                sonDict.setName(dictDto.getName());
                return sonDict;
            }).collect(Collectors.toList());
            updateBatchById(collect);
        }
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(dictDto,sysDict);
        return baseMapper.updateById(sysDict)>0;
    }

    @Override
    public boolean deleteDict(String id) {
        SysDict dict = baseMapper.selectById(id);
        if(dict!=null){
            if(StringUtils.isBlank(dict.getValue())) {
                List<SysDict> sysDicts = baseMapper.selectList(Wrappers.<SysDict>lambdaQuery().select(SysDict::getId)
                        .eq(SysDict::getName, baseMapper.selectById(dict.getId()).getName()));
                //更新所有该name的
                List<SysDict> collect = sysDicts.stream().map(sonDict -> {
                    sonDict.setDelFlag(CommonStatusEnum.DELETE.getType());
                    return sonDict;
                }).collect(Collectors.toList());
                updateBatchById(collect);
            }
        }
        dict.setDelFlag(CommonStatusEnum.DELETE.getType());
        return baseMapper.updateById(dict)>0;
    }

    @Override
    public List<SysDict> selectDictDetailList(String name) {
        return baseMapper.selectList(Wrappers.<SysDict>lambdaQuery().select(SysDict::getId, SysDict::getName, SysDict::getValue, SysDict::getLabel, SysDict::getSort).eq(SysDict::getName, name)).stream().filter(sysDict -> StringUtils.isNotEmpty(sysDict.getValue())).collect(Collectors.toList());
    }
}
